﻿using UnityEngine;
using System.Collections;

public class EffectUnitDead : MonoBehaviour
{

    public Vector3 m_kTarPos = Vector3.zero;

    public Unit _soldier;

    public float m_fAngle = 30f;
    public float m_fltMoveSpeed = 4f;
    public float m_fFlyLength = 5f;
    public Vector3 m_vForward = Vector3.zero;
    public float m_fDownDeep = -1f;
    private float _distanceToTarget = 1f;

    void Awake()
    {

    }

    void Start()
    {
        if (m_vForward.y > 0f || m_vForward.y < 0f)
        {
            m_vForward.y = 0f;
        }
        //m_vForward = -m_unit.m_vFaceDir;//new Vector3 (m_vForward.x, 0f, m_vForward.z);

        ////Debug.Log ("deadfly=====error = " + dis + "m_kTarPos == " + m_kTarPos);
    }

    public void Init()
    {
        m_kTarPos = _soldier.transform.position + m_vForward * m_fFlyLength;//+ Vector3.up * m_fDownDeep;

        //if (m_kTarPos.y > 0f)
        m_kTarPos.y = _soldier.transform.position.y;
        transform.position = _soldier.transform.position;

        _distanceToTarget = Vector3.Distance(m_kTarPos, transform.position);
     //   AEEffectMgr.Instance.LoadEffect("ArtRes\\effect\\prefab\\hit\\hit_hero001@a1_1");
     //   GameObject g = AEEffectMgr.Instance.CreateEffectByPos("ArtRes\\effect\\prefab\\hit\\hit_hero001@a1_1", transform.position, m_vForward);
    }

    void Update()
    {
        if (!BattleStage.g_InBattle)
            return;

        if (_soldier == null)
        {
            return;
        }

        float fCurrDis = Vector3.Distance(transform.position, m_kTarPos);
    
        if (fCurrDis < 0.2f)
        {
            _soldier.transform.position = m_kTarPos;
            GameObject.Destroy(gameObject);
            //m_unit.m_eUnitState = Unit.UnitState.DeadEnd;
        }
        else
        {
            transform.LookAt(m_kTarPos);
            //float angle = - m_fAngle * Mathf.Min (1, Vector3.Distance (transform.position, m_kTarPos));//m_Length
            float angle = -m_fAngle * Mathf.Min(1f, Vector3.Distance(transform.position, m_kTarPos) / _distanceToTarget);
            transform.rotation = transform.rotation * Quaternion.Euler(Mathf.Clamp(angle, -90, 90), 0, 0);
            if (m_fltMoveSpeed != 0.0f)
            {
                transform.Translate(Vector3.forward * Mathf.Min(m_fltMoveSpeed * Time.deltaTime, fCurrDis));
            }
            _soldier.transform.position = transform.position;
        }
    }
}

public class DeadStrength
{
    public DeadStrength(Unit soldier, Vector3 v, float speed, float dis, float angle, float randValue)
    {
        float min = 1f - randValue;
        float max = 1 + randValue;

        m_unit = soldier;

        _minSpeed = min * speed;
        _maxSpeed = max * speed;

        _minLength = min * dis;
        _maxLength = max * dis;

        _minAngle = min * angle;
        _maxAngle = max * angle;

        float x = Random.Range(-min, min);
        float z = Random.Range(-min, min);
        Vector3 v2 = new Vector3(x, 0f, z);
        Vector3 v3 = v + v2;
        v3.Normalize();
        _vDeadFace = v3;
    }

    private Unit m_unit = null;

    private Vector3 _vDeadFace = Vector3.zero;

    private float _minSpeed = 0f;
    private float _maxSpeed = 0f;

    private float _minLength = 1f;
    private float _maxLength = 1f;

    private float _minAngle = 30f;
    private float _maxAngle = 30f;

    public void DoDead()
    {
        GameObject objFx = new GameObject("DeadStrengthfx");
      //  objFx.transform.parent = EffectMgr.Instance.m_fxRoot.transform;

        EffectUnitDead dead = objFx.AddComponent<EffectUnitDead>();
        dead._soldier = m_unit;
        dead.m_fltMoveSpeed = Random.Range(_minSpeed, _maxSpeed);
        dead.m_fFlyLength = Random.Range(_minLength, _maxLength);
        dead.m_fAngle = Random.Range(_minAngle, _maxAngle);
        dead.m_vForward = _vDeadFace;
        dead.Init();
    }
}

